﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace EtlComm
{
    /// <summary>
    /// 任务对象
    /// </summary>
    [Serializable]
    [DebuggerDisplay("TaskID={TaskID},{SQLStatement}")]
    public class TaskObject:ICloneable
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TaskObject()
        {
            Queue4PreDay = new Queue<object>();
        }

        public string SrcIP { set; get; }
        public string DstIP { set; get; }
        /// <summary>
        /// 要执行的SQL 语句
        /// </summary>
        public string SQLStatement { get; set; }

        public string[] Params { set; get; }

        /// <summary>
        /// 任务执行次数
        /// </summary>
        public uint Executions { get; set; }
        /// <summary>
        /// 任务开始时间
        /// </summary>
        public DateTime HandlBeginTime { get; set; }
        /// <summary>
        /// 任务结束时间
        /// </summary>
        public DateTime HandlEndTime { get; set; }
        /// <summary>
        /// 加油站ID
        /// </summary>
        public string StationID { set; get; }
        /// <summary>
        /// 任务ID
        /// </summary>
        public int TaskID { set; get; }
        /// <summary>
        /// 任务名称
        /// </summary>
        public string TaskName { get; set; }
        /// <summary>
        /// 任务动作
        /// </summary>
        public SQLAction Action { set; get; }
        /// <summary>
        /// 源数据库类型
        /// </summary>
        public DataSource SrcDS { get; set; }
        /// <summary>
        /// 目标数据库类型
        /// </summary>
        public DataSource DstDS { get; set; }
        /// <summary>
        /// SQL参数的开始
        /// </summary>
        public object ParamsStart { set; get; }
        /// <summary>
        /// SQL参数的结束
        /// </summary>
        public object ParamsEnd { get; set; }
        /// <summary>
        /// 是否带有SQL参数
        /// </summary>
        public bool HasParams { set; get; }
        /// <summary>
        /// 数据库的时间
        /// </summary>
        public DateTime DBTime { set; get; }
        /// <summary>
        /// 表的最大ID
        /// </summary>
        public int MaxID { get; set; }
        /// <summary>
        /// 每次抽取的最大ID
        /// </summary>
        public int ResultMaxID { get; set; }
        /// <summary>
        /// SQL语句参数
        /// </summary>
        public IList<TaskParams> SqlParams { get; set; }
        /// <summary>
        /// 步长
        /// </summary>
        public int Step { get; set; }
        /// <summary>
        /// 步长，参数类型
        /// </summary>
        public StepType ParamType { get; set; }
        /// <summary>
        /// 主键
        /// </summary>
        public string[] PrimaryKeys { get; set; }
        /// <summary>
        /// 用来保存每天的开始点信息
        /// </summary>
        public Queue<object> Queue4PreDay { get; set; }
        /// <summary>
        /// 是否执行该任务
        /// </summary>
        public bool IsActive { get; set; }

        #region ICloneable 成员
        /// <summary>
        /// 深拷贝
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            using (Stream objectStream = new MemoryStream()) 
            { 
                IFormatter formatter = new BinaryFormatter(); 
                formatter.Serialize(objectStream, this); 
                objectStream.Seek(0, SeekOrigin.Begin); 
                return formatter.Deserialize(objectStream);
            }
        }

        #endregion
    }
    /// <summary>
    /// 加油站对象
    /// </summary>
    [DebuggerDisplay("StationID={Station.StationID}")]
    public class StationObject
    {
        /// <summary>
        /// 加油站任务队列
        /// </summary>
        public TaskQueue StationTaskQueue { get; set; }
        /// <summary>
        /// 任务列表
        /// </summary>
        public IList<TaskObject> TaskObjects { get; set; }
        /// <summary>
        /// 加油站
        /// </summary>
        public StationInfo Station { get; set; }
        /// <summary>
        /// 任务定时器
        /// </summary>
        public AutoResetEvent TaskEvent { get; set; }
        /// <summary>
        /// 加油站第CallTimes调度
        /// </summary>
        public uint CallTimes { get; set; }
        /// <summary>
        /// 加油站开始执行任务时间
        /// </summary>
        public DateTime BeginTime { get; set; }
        /// <summary>
        /// 加油站是否正在被调度
        /// </summary>
        public bool IsRuning { get; set; }
    }
    /// <summary>
    /// 
    /// </summary>
    public class TableKey
    {
        public string Name { get; set; }
        public bool IsPrimaryKey { set; get; }

        public override string ToString()
        {
            return Name;
        }
    }
    /// <summary>
    /// 任务参数
    /// </summary>
    [Serializable]
    public class TaskParams
    {
        /// <summary>
        /// 参数类型
        /// </summary>
        public Type TypeOfValue { get; set; }
        /// <summary>
        /// 参数的值
        /// </summary>
        public object Value { get; set; }
    }
}
